import {reactive} from "vue";
import myAxios from "@/plugins/axios";
import {ApiData, LoginData, RegisterData, UserModel} from "@/interfaces/apiResponse.ts";
import myLocalStore from "@/composables/myLocalStore.ts";
import router from '@/router'

import dayjs from 'dayjs';
export default  ()=>{
    const form = reactive({
        phone:'13800000001',
        password:'admin888',
        password_confirmation:'',
        captcha_code:'',
        captcha_key:'',
        code:''
    })
    const isSuperAdmin = ()=>{
        const info:UserModel = myLocalStore().get('userInfo');
        if(info){
            return info.id == 1;
        }
        return false;
    }
    const is_mine = (u_id:any)=>{
        const info:UserModel = myLocalStore().get('userInfo');
        if(info){
            return info.id == u_id;
        }
        return false;
    }
    function isLogin(){
      return !!myLocalStore().get('token');
    }
    const login = async (data:any)=>{
        try {
         const res =  await myAxios.request<ApiData<LoginData>>({
                url:'auth/login',
                method:'POST',
                data
            })
            myLocalStore().set('token',res.data.token);
            myLocalStore().set('userInfo',res.data.user);
            //是否是超级管理员，不同的跳转
            //isSuperAdmin() ? router.push({name:'admin'}) : router.push({name:'home'});
            //js DOM location path route fullPath http://bbs.test/home
            const route = router.resolve({name:'home'});
            location.href = route.fullPath;


        }catch (error){

        }
    }

    const logout = async ()=>{
        //处理后端的退出登录业务
        await myAxios.request({
            url:'logout',
            method:"POST"
        })
        myLocalStore().remove('token');
        myLocalStore().remove('userInfo');
        //router.push({name:'home'});
        location.reload();
    }
    const getRegisterCode = async (phone:any)=>{
        await myAxios.request<ApiData<{code:string}>>({
            url:'send/code',
            method:'POST',
            data:{phone}
        })
        //把发送时间存入本地存储
        myLocalStore().set('codeSendTime',dayjs())
    }
    const getRepasswordCode = async (phone:any)=>{
        await myAxios.request<ApiData<{code:string}>>({
            url:'send/repassword_code',
            method:'POST',
            data:{phone}
        })
        //把发送时间存入本地存储
        myLocalStore().set('codeSendTime',dayjs())
    }
    //发送验证码和现在的时间的差值秒数
    const diffSendTime = ()=>{
        const time = myLocalStore().get('codeSendTime');
        return time ? 60 - dayjs().diff(time,'second') : -1
    }
    const register = async (data:any)=>{
        await myAxios.request<ApiData<RegisterData>>({
            url:'auth/register',
            method:'POST',
            data
        })
    }
    const repassword = async (data:any)=>{
        await myAxios.request<ApiData<UserModel>>({
            url:'auth/repassword',
            method:'PUT',
            data
        })
    }
    return {form,login,isSuperAdmin,logout,isLogin,
        getRegisterCode,register,diffSendTime,
        getRepasswordCode,repassword,is_mine};
}